package cn.dslcode.security.config.oauth2.resourceserver;

import cn.dslcode.security.config.CustomAuthenticationHandlers;
import cn.dslcode.security.config.CustomSecurityConstant;
import cn.dslcode.security.config.oauth2.OAuth2BeanConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfiguration;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurer;

import java.util.Arrays;
import java.util.List;

@Configuration
public class ResourceServerConfig {

    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private OAuth2BeanConfiguration oauth2Beans;
    @Autowired
    private CustomAuthenticationHandlers authenticationHandlers;
    @Autowired
    private CustomSecurityConstant securityConstant;

    @Bean
    protected ResourceServerConfiguration bataResourceServer(OAuth2BeanConfiguration oAuth2Bean) {
        ResourceServerConfiguration resource = new ResourceServerConfiguration() {
            public void setConfigurers(List<ResourceServerConfigurer> configurers) {
                super.setConfigurers(configurers);
            }
        };
        GoodsResourceServerConfigurer resourceServerConfigurer = new GoodsResourceServerConfigurer(
            "goods-services",
            oAuth2Bean.tokenStore(),
            null
//            new CustomCaptchaVerifyFilter(
//                authenticationManager,
//                authenticationHandlers.getAuthenticationSuccessHandler(),
//                authenticationHandlers.getAuthenticationFailureHandler(),
//                securityConstant.getUri().getLoginForm(),
//                securityConstant.getParameterName().getCaptchaCode()
//            )
        );
        resource.setConfigurers(Arrays.asList(resourceServerConfigurer));
        resource.setOrder(3);
        return resource;

    }

    @Bean
    protected ResourceServerConfiguration userResourceServer(OAuth2BeanConfiguration oAuth2Bean) {
        ResourceServerConfiguration resource = new ResourceServerConfiguration() {
            public void setConfigurers(List<ResourceServerConfigurer> configurers) {
                super.setConfigurers(configurers);
            }
        };
        resource.setConfigurers(Arrays.asList(new UserResourceServerConfigurer("user-services", oAuth2Bean.tokenStore())));
        resource.setOrder(4);
        return resource;
    }

}